import myglobal
from PySide6.QtCore import QIODevice,QFile,QTextStream
class MySig():
# extern QStringList *pmychannelNames;
# extern QHash<QString, ChannelAD> *pgchannels;
    def writeFileLines(self,fp,ls):
        f=QFile(fp)
        f.open(QIODevice.WriteOnly)
        stream=QTextStream(f)
        i=0;
        while(i<len(ls)): #(i=0;i<ls.size();i++)
            stream<<ls[i]+'\n';
            i+=1
    def readLines(self,fp):
        f=QFile(fp)
        f.open(QIODevice.ReadOnly)
        stream=QTextStream(f)
        # QString line;
        # QStringList ls;
        ls=[]
        line = stream.readLine()
        # print(line+")")
        # input("hi")
        while(line!=""):
            ls.append(line)
            line=stream.readLine()
            # print(line+")")
            # input("hi")
        return ls
    def __init__(self):
        self.name="";
        self.weight=0.123;
        self.xn=0;
        self.numChannels=4;
        self.channels=[]#QByteArray[numChannels];
        for i in range(self.numChannels):
            self.channels.append([])
        # curves=new QwtPlotCurve[numChannels];
        # int n=pmychannelNames->size();
        # for (int i=0;i<n;i++)
        # {
        #     QColor c(QRgb((*pgchannels)[(*pmychannelNames)[i]].color));
        #     qDebug()<<c;
        #     (curves+i)->setTitle((*pmychannelNames)[i]);
        #     (curves+i)->setPen(c);
        # }

    #(curves+2)->setPen(QPen(QColor((*pgchannels)["C"].color)));
    #(curves+3)->setPen(QPen(QColor((*pgchannels)["C"].color)));

# }
# CMySig::~CMySig()
# {
#     delete[] channels;
#     delete[] curves;
# }
# #void showd(double * x,double * y ,long n){
# #    for(int i=0;i<n;i++){
# #        qDebug()<<*(x+i);
# #        qDebug()<<*(y+i);
# #    }

# #}
# void CMySig::show(QwtPlot *plot)
# {
#     if(xn>0)
#     {
#         for(int i=0;i<numChannels;i++)
#         {
#             (curves+i)->attach(plot);
# #            showd((double *)mtime.constData(), (double *)channels[i].constData(), xn);
#             (curves+i)->setRawSamples((double *)mtime.constData(), (double *)channels[i].constData(), xn);
#         }
#     }
# }
# bool CMySig::haveData()
# {
#     return xn>0;
# }
# #void CMySig::setTimeArr(QByteArray timeArr)
# #{
# #    mtime=timeArr;
# #}
# void CMySig::setXn(long n)
# {
#     xn=n;
#     mtime.resize(xn*8);
#     for (long i=0;i<numChannels;i++)
#     {
#         channels[i].resize(xn*8);#double
#     }
# }
# QStringList CMySig::toStringList()
# {
#     QStringList l;
#     if(xn>0)
#     {
#         qDebug()<<xn;
#         double *p;
#         double * volt[numChannels];
#         p=(double *)mtime.constData();
#         for(int j=0;j<numChannels;j++)
#         {
#             volt[j]=(double *)channels[j].constData();
#         }
#         for(long i=0;i<xn;i++)
#         {
#             QString voltStr,tmpStr;
#             #voltStr=tmpStr.setNum(i);
#             voltStr=tmpStr.setNum(*p);
#             for(int j=0;j<numChannels;j++)
#             {
#                 voltStr+="\t"+tmpStr.setNum(*volt[j]);
#             }
#             p++;
#             for(int j=0;j<numChannels;j++)
#             {
#                 volt[j]++;
#             }
#             qDebug()<<voltStr;
#             l.append(voltStr);
#         }
#     }
#     return l;
# }

# #void CMySig::setVoltArr(QByteArray voltArr, int index)
# #{
# #    if (index<numChannels && index>=0)
# #    {
# #        channels[index]=voltArr;
# #    }
# #}
# void CMySig::writeFile(QString fp)
# {
#     QFile f(fp);
#     f.open(QIODevice::WriteOnly);
#     QDataStream stream(&f);
#     qint8 tmp;
#     tmp=xn;
#     stream<<tmp;
#     double *p;
#     double * volt[numChannels];
#     p=(double *)mtime.constData();
#     for(int j=0;j<numChannels;j++)
#     {
#         volt[j]=(double *)channels[j].constData();
#     }
#     for(long i=0;i<xn;i++)
#     {
#         stream<<*p;
#         for(int j=0;j<numChannels;j++)
#         {
#             stream<<*volt[j];
#         }
#         p++;
#         for(int j=0;j<numChannels;j++)
#         {
#             volt[j]++;
#         }
#     }
#     f.close();
# }
    def testData(self):
        self.name="hello";
        self.weight=0.123;
        self.xn=100;
        self.mtime=[]#.resize(xn*sizeof(double));
        for i in range(self.numChannels):# (i=0;i<numChannels;i++):
            self.channels[i]=[]
            # channels[i]=[]
        for j in range(self.xn):#(long i=0;i<xn;i++)
            for i in range(self.numChannels):# (i=0;i<numChannels;i++):
                self.channels[i].append(i*((j+1.0)/self.numChannels))
# void CMySig::readFile(QString fp)
# {
#     QFile f(fp);
#     f.open(QIODevice::ReadOnly);
#     qint8 tmp;
#     QDataStream stream(&f);
#     stream>>tmp;
#     xn=tmp;
#     qDebug()<<xn;
#     int i=0;
#     mtime.resize(xn*8);
#     for (i=0;i<numChannels;i++)
#     {
#         channels[i].resize(xn*8);
#     }
#     double *p;
#     double * volt[numChannels];
#     p=(double *)mtime.constData();
#     for(int j=0;j<numChannels;j++)
#     {
#         volt[j]=(double *)channels[j].constData();
#     }
#     for(long i=0;i<xn;i++)
#     {
#         QString voltStr,tmpStr;
#         stream>>*p;
#         for(int j=0;j<numChannels;j++)
#         {
#             stream>>*volt[j];
#         }
#         p++;
#         for(int j=0;j<numChannels;j++)
#         {
#             volt[j]++;
#         }
#     }
#     f.close();
# }
# void CMySig::readTextFile(QString fp){
#     QStringList lines=readLines(fp);
#     name=lines[0];
#     weight=lines[1].toDouble();
#     xn=lines.size()-3;
#     mtime.resize(xn*8);
#     for (long i=0;i<numChannels;i++)
#     {
#         channels[i].resize(xn*8);
#     }
#     double *p;
#     double * volt[numChannels];
#     p=(double *)mtime.constData();
#     for(int j=0;j<numChannels;j++)
#     {
#         volt[j]=(double *)channels[j].constData();
#     }
#     for(long i=2;i<xn+2;i++)
#     {
#         QString line=lines[i];
#         QStringList ws=line.split("\t");
#         qDebug()<<line;
#         qDebug()<<ws;
#         if (ws.size()<5)
#         {
#             qDebug()<<"no enough channels";
#         }
#         else
#         {
#             *p=ws[0].toDouble();
#             for(int j=0;j<numChannels;j++)
#             {
#                 *volt[j]=ws[1+j].toDouble();
#             }
#             p++;
#             for(int j=0;j<numChannels;j++)
#             {
#                 volt[j]++;
#             }
#         }
#     }
#     qDebug()<<xn;    
# }
# void CMySig::readTextFile_new(QString fp)
# {
#     # char * ch;
#     # ch=fp.toLatin1().data();
#     # printf("%s",ch);
#     qDebug()<<fp;
#     QStringList lines=readLines(fp);
#     # qDebug()<<lines;
#     name=lines[2];
#     weight=lines[1].toDouble();
#     xn=lines.size()-11;
#     mtime.resize(xn*8);
#     for (long i=0;i<numChannels;i++)
#     {
#         channels[i].resize(xn*8);
#     }
#     double *p;
#     double * volt[numChannels];
#     p=(double *)mtime.constData();
#     for(int j=0;j<numChannels;j++)
#     {
#         volt[j]=(double *)channels[j].constData();
#     }
#     for(long i=4;i<xn+4;i++)
#     {
#         QString line=lines[i];
#         QStringList ws=line.split(",");
#         qDebug()<<line;
#         qDebug()<<ws;
#         if (ws.size()<5)
#         {
#             qDebug()<<"no enough channels";
#         }
#         else
#         {
#             *p=ws[0].toDouble();
#             for(int j=0;j<numChannels;j++)
#             {
#                 *volt[j]=ws[1+j].toDouble();
#                 qDebug("%lf",*volt[j]);
#             }
#             p++;
#             for(int j=0;j<numChannels;j++)
#             {
#                 volt[j]++;
#             }
#         }
#     }
#     qDebug()<<xn;
# }
# void CMySig::writeTextFile(QString fp)
# {
#     QFile f(fp);
#     f.open(QIODevice::WriteOnly);
#     QTextStream stream(&f);
#     stream<<name<<"\n";#name
#     stream<<weight<<"\n";#weight
#     QStringList l;
#     l=this->toStringList();
#     for(long i=0;i<l.size();i++)
#     {
#         stream<<l[i]<<"\n";
#     }
#     f.close();
# }
# void CMySig::saveSig(long id)
# {
#     QSqlRecord record;

#     QSqlField f1("sampleid", QVariant::Int);
#     QSqlField f2("mtimeArr", QVariant::ByteArray);
#     QSqlField f3("cvoltArr", QVariant::ByteArray);
#     QSqlField f4("svoltArr", QVariant::ByteArray);
#     QSqlField f5("c2voltArr", QVariant::ByteArray);
#     QSqlField f6("s2voltArr", QVariant::ByteArray);
#     QSqlField f7("xn", QVariant::Int);

#     f1.setValue(QVariant((int)id));
#     f2.setValue(QVariant(mtime));
#     f3.setValue(QVariant(channels[0]));
#     f4.setValue(QVariant(channels[1]));
#     f5.setValue(QVariant(channels[2]));
#     f6.setValue(QVariant(channels[3]));
#     f7.setValue(QVariant((int)xn));

#     record.append(f1);
#     record.append(f2);
#     record.append(f3);
#     record.append(f4);
#     record.append(f5);
#     record.append(f6);
#     record.append(f7);

#     QSqlTableModel* model = new QSqlTableModel(NULL);
#     model->setTable("voltnew");
#     model->insertRecord(-1, record);
# }
# void CMySig::loadSig(long id)
# {
#     QString cmd;
#     QString idstr;
#     idstr.setNum(id);
#     cmd="SELECT * FROM voltnew where sampleid="+idstr;
#     qDebug()<<cmd;
#     QSqlQuery q(cmd);
#     QSqlRecord rec;# = q.record();
#     while (q.next())
#     {
#         rec = q.record();
#         xn=(rec.value("xn").toInt());
#         mtime=rec.value("mtimeArr").toByteArray();
#         channels[0]=rec.value("cvoltArr").toByteArray();
#         channels[1]=rec.value("svoltArr").toByteArray();
#         channels[2]=rec.value("c2voltArr").toByteArray();
#         channels[3]=rec.value("s2voltArr").toByteArray();
#     }
# }
# void CMySig::saveAll()
# {
# }
# void CMySig::setOneData(QList<double> l,long at)
# {
#     if (at<0) return;
#     if (at>=0 && at<xn)
#     {
#         double *p;
#         double * volt[numChannels];
#         p=(double *)mtime.constData();
#         for(int j=0;j<numChannels;j++)
#         {
#             volt[j]=(double *)channels[j].constData();
#         }

#         *(p+at)=l[0];
#         for(int j=0;j<numChannels;j++)
#         {
#             *(volt[j]+at)=l[j+1];
#         }
#     }
#     else
#     {
#             setXn(at+1);
#             double *p;
#             double * volt[numChannels];
#             p=(double *)mtime.constData();
#             for(int j=0;j<numChannels;j++)
#             {
#                 volt[j]=(double *)channels[j].constData();
#             }

#             *(p+at)=l[0];
#             for(int j=0;j<numChannels;j++)
#             {
#                 *(volt[j]+at)=l[j+1];
#             }
#     }
# }
# long CMySig::getXn()
# {
#     return xn;
# }
# void CMySig::detach()
# {
#     if(xn>0)
#     {
#         for(int i=0;i<numChannels;i++)
#         {
#             (curves+i)->detach();
#         }
#     }
# }
# void CMySig::clone(CMySig *sig2)
# {
#     qDebug()<<sig2->getXn();
#     setXn(sig2->getXn());
#     void *des;void *src;
#     des=(void *)mtime.constData();
#     src=(void *)sig2->mtime.constData();
#     memcpy(des,src,xn*sizeof(double));
#     for (long i=0;i<numChannels;i++)
#     {
#         #channels[i].resize(xn*8);
#         des=(void *)channels[i].constData();
#         src=(void *)sig2->channels[i].constData();
#         memcpy(des,src,xn*sizeof(double));

#     }
# }
if __name__=="__main__":
    s=MySig()
    # s.writeFileLines("a.txt",["ab","b"])
    # print(s.readLines("a.txt"))
    s.testData()